package com.zhangshuo.controller;

import com.zhangshuo.common.shiro.RetryLimitCredentialsMatcher;
import com.zhangshuo.common.shiro.ShiroUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/1 0001.
 */
@Controller
public class HomeController {


    @RequestMapping("/home")
    @ResponseBody
    public Object home() {
        return "Welcome Home Page";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("login")
    @ResponseBody
    public Object login(
            @RequestParam() String username,
            @RequestParam() String password) {
        Map<String, Object> result = new HashMap();
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);
        } catch (AuthenticationException exception) {
            result.put("message", exception.getMessage());
            return result;
        }
        result.put("message","成功");
        return result;

    }

    /**
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/index";
    }


    @RequestMapping("/index")
    public ModelAndView index(ModelAndView mv) {
        mv.getModel().put("name","张三");
        mv.setViewName("index");
        return mv;
    }


    @RequestMapping("/index2")
    public ModelAndView index2(ModelAndView mv) {
        mv.getModel().put("name","张三");
        mv.setViewName("index");
        return mv;
    }

}
